#  Copyright (c) 2020 - ~, WANGXI
#  
#  WIRELESS UDP TCL FILE
#  Used in NS-2 simulator
#  Clone from https://gitee.com/WangXi_Chn/ns2_modulelib
#  Note in https://www.yuque.com/wangxi_chn/kozrfl
#  
#  Change Logs:
#  Date           Author       Notes			Mail
#  2020-11-24     WangXi   	   first version	WangXi_Chn@foxmail.com

# UDP modulation in wireless condition

# module choose
set val(prop)   Propagation/TwoRayGround    ;#radio-propagetion model
set val(netif)  Phy/WirelessPhy             ;#network interface type
set val(mac)    Mac/802_11                  ;#MAC type
set val(ifq)    Queue/DropTail/PriQueue     ;#interface queue type
set val(ll)     LL                          ;#link layer type
set val(ant)    Antenna/OmniAntenna         ;#antenna modul
set val(ifqlen) 50                          ;#max packet in ifq
set val(rp)     DSDV

# build a simulator instance
set ns_ [new Simulator]

# build topology object
set topo [new Topography]
$topo load_flatgrid 700 700

# setup trace
set tr [open wireless_udp.tr w]
$ns_ trace-all $tr
$ns_ eventtrace-all
set nf [open wireless_udp.nam w]
$ns_ namtrace-all-wireless $nf 700 700

# create god
create-god 2

# config wireless node
$ns_ node-config    -adhocRouting $val(rp) \
                    -llType $val(ll) \
                    -macType $val(mac) \
                    -ifqType $val(ifq) \
                    -ifqLen $val(ifqlen) \
                    -antType $val(ant) \
                    -propType $val(prop) \
                    -phyType $val(netif) \
                    -channelType Channel/WirelessChannel \
                    -topoInstance $topo \
                    -agentTrace ON \
                    -routerTrace OFF \
                    -macTrace ON \
                    -movementTrace OFF

# create node
set node_(0) [$ns_ node]
set node_(1) [$ns_ node]

# node lable and set original position
$node_(0) label "Node 0"
$node_(0) set X_ 10.0
$node_(0) set Y_ 10.0
$node_(0) set Z_ 0.0

$node_(1) label "Node 1"
$node_(1) set X_ 600.0
$node_(1) set Y_ 600.0
$node_(1) set Z_ 0.0

# bulid CBR service baced on UDP 
set udp1 [new Agent/UDP]
set null1 [new Agent/Null]
$ns_ attach-agent $node_(0) $udp1
$ns_ attach-agent $node_(1) $null1
$ns_ connect $udp1 $null1
set cbr1 [new Application/Traffic/CBR]
$cbr1 set rate_ 0.5Mb
$cbr1 set packetSize_ 200
$cbr1 attach-agent $udp1
$ns_ at 5.0 "$cbr1 start"

$ns_ at 0.0 "$node_(0) reset"
$ns_ at 0.0 "$node_(1) reset"

# movement of node
$ns_ at 0.6 "$node_(0) setdest 300.0 250.0 60.0"
$ns_ at 1.1 "$node_(1) setdest 300.0 300.0 60.0"
$ns_ at 40  "$node_(0) setdest 600.0 600.0 20.0"
$ns_ at 40.1 "$node_(1) setdest 100.0 100.0 20.0"

# envent build
$ns_ at 50.0 "finish"
$ns_ at 50.1 "puts \"NS EXITING\";$ns_ halt"

proc finish {} {
    global ns_ tr nf val
    $ns_ flush-trace
    close $tr
    close $nf 
    # run nam to display
    exec nam wireless_udp.nam &
    exit 0
}

puts "Starting Simulation..."
$ns_ run 

#/************************ (C) COPYRIGHT 2020 WANGXI **************END OF FILE****/
